﻿using ApplicationModels.A05.Reports;
using Commons.Enums;
using Commons.Helpers;
using Commons.Requests;
using Commons.Responses;
using EntityModels.E05.Reports;
using IApplicationServices;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;

namespace WebAPI.Controllers
{
    [Route("[controller]/[action]")]
    public class CommentReportController : BaseController<int, CommentReport, CommentReportAM>
    {
        private readonly ICommentReportService _commentReportService;
        public CommentReportController(ICommentReportService apiService) : base(apiService)
        {
            _commentReportService = apiService;
        }
        public override Task<MessageResponse> AddAsync(CommentReportAM apiEntity)
        {
            apiEntity.ReporterId = AppUserId;
            return base.AddAsync(apiEntity);

        }

        [HttpPost]
        public async Task<MessageResponse> ExamineAsync(CommentReportAM req)
        {
            //ReportStatusEnum status;
            //if (Enum.TryParse(req.StatusName, true, out status))
            //{
            //    return await _commentReportService.ReportExamine(req.Id, status);
            //}
            //return new MessageResponse("参数异常!");
            return await _commentReportService.ReportExamine(req.Id, req.Status);
        }

        public override async Task<DatasResponse<CommentReportAM>> GetBySelectAsync([FromQuery] PagingRequest req)
        {
            Expression<Func<CommentReport, bool>> filter = x => true;
            if (!string.IsNullOrWhiteSpace(req.SearchValue) && Enum.TryParse(req.SearchValue, out ReportStatusEnum status))
            {
                filter = x=>x.Status == status;
            }
            if (!string.IsNullOrWhiteSpace(req.ExtendedValue1))
            {
                filter = filter.And(x => x.Comment.AppUser.NickName.Contains(req.ExtendedValue1));
            }
            if (!string.IsNullOrWhiteSpace(req.ExtendedValue2))
            {
                filter = filter.And(x => x.Reporter.NickName.Contains(req.ExtendedValue2));
            }
            if (!string.IsNullOrWhiteSpace(req.ExtendedValue3) && Int32.TryParse(req.ExtendedValue3,out int e3))
            {
                filter = filter.And(x => x.ReportTypeId == e3);
            }
            return await ApiService.GetBySelectAsync(req.PageIndex, req.PageSize, filter);
        }
    }
}
